Ext.define('appmod.controller.knowledge.category.fileformat.Startup', {
    extend: 'Ext.app.Controller',

    views: [
    	'knowledge.category.fileformat.DefaultListView',
		'knowledge.category.fileformat.Form'
    ],
	models : [
		'knowledge.category.fileformat.Format'
	],
	
    stores: [
		'knowledge.category.fileformat.AllList'
	],	

    refs: [
		{ref: 'defaultListView',selector:'knowledge-category-fileformat-list'},
		{ref: 'searchListView',selector:'knowledge-category-fileformat-list searchfield'}
	],

    init: function() {

    	this.control({
			'knowledge-category-fileformat-list menuitem[action=addFileFormat]': {
				click:{
					fn:this.showAddFileFormatForm,
					scope:this
				}
			},
			'defaultlistview menuitem[action=updatesimpleuser]': {
				click:this.showUpdateSimpleUserInfoForm
			},			
			'defaultlistview menuitem[action=selectroles]': {
				click:this.showSelectRolesform
			},
			'defaultlistview menuitem[action=deleteusers]': {
				click:this.showDeleteusersDialog
			}
			
						

    	});


    },
	
		
	loadInitComp : function() {
		var mainPanel = Ext.create('appmod.view.knowledge.category.fileformat.DefaultListView' , {
			region : 'center',
			border:false,
			padding:'0 0 0 0'
		});
		return mainPanel;		
	},
	
	onLaunch:function(app) {
		
		var me = this;

		var defaultListView = me.getDefaultListView();
		var dsDListView = me.getKnowledgeCategoryFileformatAllListStore();
		defaultListView.bindStore( dsDListView );

		
	},
	
	// create fileformat
	showAddFileFormatForm : function() {
		var me = this;
		var win = null;
		// --- build new form ---
		if (!win) {
			win = new Ext.create('widget.window', {
				title : '文件格式',
				closable : true,
				//closeAction:'hide',
				modal:true,
				width : 450,
				height : 200,
				layout: 'border',
				bodyStytle : 'padding : 5px',
				items:[{
					padding : 5,
					defaults: {
        				labelWidth:65
    				},
					xtype : 'knowledge.category.fileformat.form',
					region:'center'
				}],
				dockedItems: [{
				    xtype: 'toolbar',
				    dock: 'bottom',
				    ui: 'footer',
				    items: [
						'->',
				        {
            				text: '保 存',
							action:'addFileFormat'
        				}
				    ]
				}]	
			});
		}
		
		// --- find form ---
		var form = win.down("knowledge-category-fileformat-form");

		var saveBtn = win.down('button[action="addFileFormat"]');
		saveBtn.on("click",me.saveNewFileFormat , me , {
			window : win
		});

		if ( !win.isVisible() ) {
			win.show();
		}
		
	},
	
	saveNewFileFormat:function(btn , event , eOpts) {
		var me = this;
		var listView = me.getDefaultListView();
		var dsUser = listView.getStore();

		var upWin = eOpts.window;
		
		var formPanel = upWin.down('knowledge-category-fileformat-form');
		var win = upWin;
		
		var form = formPanel.getForm();
		form.submit({
			url:'../../data/resource/knowledge/category/fileformat/submit_adding.json',
			success:function(form , action) {
				 // --- reflash store ---
				 win.close();
				 
				 dsUser.load();
				 
				 Ext.Msg.alert('成功', action.result.msg);
			},
			failure: function(form, action){
				switch (action.failureType) {
					case Ext.form.action.Action.CLIENT_INVALID:
						Ext.Msg.alert('失败', '表单里包含错误的信息');
						break;
					case Ext.form.action.Action.CONNECT_FAILURE:
						Ext.Msg.alert('失败', '服务器连接失败,请联系管理人员');
						break;
					case Ext.form.action.Action.SERVER_INVALID:
						Ext.Msg.alert('失败', action.result.msg);
				}
			}
		});
	}
	
	
});